package com.gulimall.order.service.impl;

import com.common.to.MemberTo;
import com.gulimall.order.entity.OrderEntity;
import com.gulimall.order.interceptor.OrderInterceptor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.gulimall.order.dao.OrderItemDao;
import com.gulimall.order.entity.OrderItemEntity;
import com.gulimall.order.service.OrderItemService;

import javax.annotation.Resource;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Resource
    private OrderServiceImpl orderServiceImpl;
    @Resource
    private OrderItemDao orderItemDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getItemList(Map<String, Object> requestParam) {
        // 1、计算订单分页数据
        MemberTo memberTo = OrderInterceptor.userMap.get();
        IPage<OrderEntity> orderPage = orderServiceImpl.page(new Query<OrderEntity>().getPage(requestParam),
                new QueryWrapper<OrderEntity>().eq("member_id", memberTo.getId()).orderByDesc("create_time")
        );

        // 2、破解计算订单项数据
        orderPage.getRecords().forEach(orderEntity -> {
            List<OrderItemEntity> orderItemList = orderItemDao.selectList(new QueryWrapper<OrderItemEntity>().eq("order_id", orderEntity.getId()));
            orderEntity.setOrderItemEntityList(orderItemList);
        });

        return new PageUtils(orderPage);
    }

}